import axios from 'axios'
import { getMapRegionByCode, getMapRegionCode } from '@/api/portal/regionConfig'

export default {
  mmConfig: {
    'extent': [110, 42, 140, 53],
    'mapRegion': {
      f_levels: -1,//先默认给3，为了控制图层的显示范围，给一个mask
      f_regioncode: 'f_regioncode',
      f_userid: -1,
      f_username: 'f_username',
      f_name: '',
      prjcode: 'EPSG:4490',
      userPrjCode: 'EPSG:4490',
      f_xmax: 140,
      f_xmin: 110,
      f_ymax: 53,
      f_ymin: 42,
      geomwkt: ''
    }
  },
  getRegionAndSettingAxios() {
    let that = this
    //已经初始化了就直接返回
    if (that.mmConfig.mapRegion.f_levels != -1) {
      return
    }
    //必须都是4490，不管别的
    axios.all([getMapRegionCode()]).then(axios.spread((region) => {
      let mr = region.data.mapRegionCode[0]
      that.mmConfig.mapRegion.userPrjCode = 'EPSG:4490'//用户选择的地图投影坐标系
      that.mmConfig.mapRegion.f_regioncode = mr.f_regioncode
      that.mmConfig.mapRegion.f_userid = mr.f_userid
      that.mmConfig.mapRegion.f_username = mr.f_username
      that.mmConfig.mapRegion.f_levels = mr.f_levels
      that.mmConfig.mapRegion.f_name = mr.f_name
      if (mr.f_levels === 1) {
        that.initCore()
      } else {
        that.queryRegionAxios(that.mmConfig.mapRegion.f_regioncode)
      }
    })).catch(function(error) {
      console.log('getRegionAndSettingAxios', error)
    })
  },
  //免登录默认给一个
  getRegionAndSettingAxiosNotLgion() {
    let that = this
    //已经初始化了就直接返回

    that.mmConfig.mapRegion.userPrjCode = 'EPSG:4490'//用户选择的地图投影坐标系
    that.mmConfig.mapRegion.f_regioncode = '230000'
    that.mmConfig.mapRegion.f_userid = 387
    that.mmConfig.mapRegion.f_username = 'admin'
    that.mmConfig.mapRegion.f_levels = 1
    that.mmConfig.mapRegion.f_name = '绥化市'
    that.mmConfig.extent=[126.8416,46.5298,127.0941,46.7475];

  },
  /* 数据初始化*/
  initCore() {
    let that = this
    //判断当前坐标系如果是平面 需要转投影
    let dataPrjCode = that.mmConfig.mapRegion.prjcode
    let mapPrjCode = that.mmConfig.mapRegion.userPrjCode
    if (dataPrjCode != mapPrjCode) {
      var point1 = ol.proj.transform([parseFloat(that.mmConfig.extent[0]), parseFloat(that.mmConfig.extent[1])], dataPrjCode, mapPrjCode)
      var point2 = ol.proj.transform([parseFloat(that.mmConfig.extent[2]), parseFloat(that.mmConfig.extent[3])], dataPrjCode, mapPrjCode)
      that.mmConfig.extent = [point1[0], point1[1], point2[0], point2[1]]
    }

  },
  /*
* 查询用户范围ie
* @param {type} param 参数
* @return {jqXHR}
*/
  queryRegionAxios(xzqCode) {
    let that = this
    getMapRegionByCode(xzqCode).then(function(res) {
      let rd = res.data.regions[0]
      that.mmConfig.mapRegion.f_name = rd.f_name
      that.mmConfig.mapRegion.f_xmax = rd.f_xmax
      that.mmConfig.mapRegion.f_xmin = rd.f_xmin
      that.mmConfig.mapRegion.f_ymax = rd.f_ymax
      that.mmConfig.mapRegion.f_ymin = rd.f_ymin
      that.mmConfig.mapRegion.extent = [parseFloat(that.mmConfig.mapRegion.f_xmin), parseFloat(that.mmConfig.mapRegion.f_ymin),
        parseFloat(that.mmConfig.mapRegion.f_xmax), parseFloat(that.mmConfig.mapRegion.f_ymax)]
      that.mmConfig.mapRegion.geomwkt = rd.geomwkt//mapRegion.geomwkt已经被watch了。
      that.initCore()

    })
      .catch(function(error) {
        console.log(error)
      })
  },
  getQueryString(name) {
    let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
    let r = window.location.search.substr(1).match(reg)
    if (r != null) return unescape(r[2])
    return null
  }
}




